import { reactive } from 'vue'
import { user_login } from '@/api/login'
import { useUserInfoStore } from '@/stores/userInfo'
const { setUserInfo } = useUserInfoStore()
import { setUserInfo as l_setUserInfo, setToken } from '@/utils/auth'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { type UserInfoInter } from '@/types/userInfo'
export default function () {
  let form = reactive({
    member_phone: '',
    password: ''
  })
  const router = useRouter()

  async function login() {
    user_login(form)
      .then((response) => {
        setUserInfo(response as UserInfoInter)
        l_setUserInfo(response as UserInfoInter)
        setToken((response as UserInfoInter).token)
        ElMessage.success('登录成功')
        setTimeout(() => {
          router.go(-1)
        }, 2000)
      })
      .catch((error: string) => {
        console.error(error)
      })
  }
  return {
    form,
    login
  }
}
